import { BadRequestException, ParseIntPipe } from '@nestjs/common';
import * as crypto from 'crypto';
import { LoginUserVo } from './user/vo/login-user.vo';

export function md5(str) {
    const hash = crypto.createHash('md5');
    hash.update(str);
    return hash.digest('hex');
}
export function generateParseIntPipe(name) {
    return new ParseIntPipe({
        exceptionFactory() {
            throw new BadRequestException(name + ' 应该传数字');
        }
    })
}
export function setLoginVo(vo: LoginUserVo) {
    vo.accessToken = this.jwtService.sign({
        userId: vo.userInfo.id,
        username: vo.userInfo.username,
        roles: vo.userInfo.roles,
        permissions: vo.userInfo.permissions
    }, {
        expiresIn: this.configService.get('jwt_access_token_expires_time') || '30m'
    });

    vo.refreshToken = this.jwtService.sign({
        userId: vo.userInfo.id
    }, {
        expiresIn: this.configService.get('jwt_refresh_token_expres_time') || '7d'
    });
}